package com.wangxs.cry.dec.controller;

import com.wangxs.cry.dec.annotion.CryDecToParam;
import com.wangxs.cry.dec.commom.ServiceResult;
import com.wangxs.cry.dec.params.BaseParam;
import com.wangxs.cry.dec.params.UserParams;
import com.wangxs.cry.dec.service.IUserService;
import com.wangxs.cry.dec.utils.CryDecUtils;
import com.wangxs.cry.dec.utils.ValidateParamsUtil;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.script.ScriptException;

@RequestMapping
@RestController
public class UserController {

    @Autowired
    private IUserService userService;

    @CryDecToParam
    @PostMapping("cry/1")
    public ServiceResult test(@RequestBody String params){
        ServiceResult add = userService.add(ValidateParamsUtil.validate(params,
                UserParams.class,
                BaseParam.add.class));
        return add;
    }

/*    @CryDecToParam
    @PostMapping("cry/2")
    public ServiceResult edit(@RequestBody String params){
        ServiceResult add = userService.edit(ValidateParamsUtil.validate(params,
                UserParams.class,
                BaseParam.edit.class));
        return add;
    }*/

    public static void main(String[] args) {
        crytest1();
    }

    /**
     * 例子
     */
    private static void crytest1() {
        try {
            String aa = "{\n" +
                    "    \"id\":\"224\",\n" +
                    "    \"name\":\"摩擦的\",\n" +
                    "    \"makno\":\"222253\"\n" +
                    "}";
            String str = CryDecUtils.doSm2Cry(aa);
            String s = Base64.encodeBase64String(str.getBytes());
//            String s2 = CryDecUtils.doSm2Cry(s);
            System.out.println(s);
//            System.out.println(s2);
        } catch (ScriptException e) {
            e.printStackTrace();
        }
    }


}
